﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.Net.Mail;
using eschoolBO;

namespace ESCHOOL.admin
{
    public partial class WebForm4 : System.Web.UI.Page
    {
        teacher teach = new teacher();
        user_types ut = new user_types();
        protected void Page_Load(object sender, EventArgs e)
        {
            
            btnupdate.Visible = false;
            if (!IsPostBack)
            {
                for (int i = 1; i <= 31; i++)
                    dddate.Items.Add(i.ToString());
                for (int j = 1950; j < 2050; j++)
                    ddyear.Items.Add(j.ToString());
                for (int k = 1; k <= 31; k++)
                    ddjdate.Items.Add(k.ToString());
                for (int j = 1950; j < 2050; j++)
                    ddjyear.Items.Add(j.ToString());
                DataTable dt1 = ut.GetRecords(-99, "", false, " and ");
                ddusertype.DataSource = dt1;
                ddusertype.DataTextField = "user_type";
                ddusertype.DataValueField = "user_type_id";
                ddusertype.DataBind();
                DataTable dt = teach.GetRecords(-99, "", "", "", "", "", "", -99, -99, -99, "", "","","","", false, " and ");
                GridView1.DataSource = dt;
                GridView1.DataBind();
            }
        }

        protected void btnedit_Command(object sender, CommandEventArgs e)
        {
            btninsert.Visible = false;
            btnupdate.Visible = true;
            Label1.Visible = false; Label2.Visible = false;
            DataTable dt = teach.GetRecords(Convert.ToInt64(e.CommandArgument), "", "", "", "", "", "", -99, -99, -99, "", "","","","", false, " and ");
            tbtid.Text = dt.Rows[0]["teacher_id"].ToString();
            tbtname.Text = dt.Rows[0]["teacher_name"].ToString();
            DateTime dat=new DateTime();
            dat=Convert.ToDateTime(dt.Rows[0]["date_of_birth"].ToString());
            dddate.SelectedValue = dat.Day.ToString();
            ddmonth.SelectedValue = dat.Month.ToString();
            ddyear.SelectedValue = dat.Year.ToString();
            DateTime dat2 = new DateTime();
            dat2 = Convert.ToDateTime(dt.Rows[0]["date_of_join"].ToString());
            ddjdate.SelectedValue = dat2.Day.ToString();
            ddjmonth.SelectedValue = dat2.Month.ToString();
            ddjyear.SelectedValue = dat2.Year.ToString();
            RadioButtonList1.SelectedValue = dt.Rows[0]["gender"].ToString();
            tbphoto.Text = dt.Rows[0]["photo"].ToString();
            Image2.Visible = true;
            Image2.ImageUrl = "~/admin/teacher/" + tbphoto.Text;
            tbaddress.Text = dt.Rows[0]["permanent_address"].ToString();
            tbreno.Text=dt.Rows[0]["residence_pno"].ToString();
            tbmobile.Text = dt.Rows[0]["mobile_pno"].ToString();
            ddusertype.SelectedValue = dt.Rows[0]["user_type_id"].ToString();
            tbmail.Text = dt.Rows[0]["teacher_emailid"].ToString();
            tbusername.Visible = false;
            tbpassword.Visible = false;
            tbusername.Text = dt.Rows[0]["username"].ToString();
            tbpassword.Text = dt.Rows[0]["password"].ToString();
            tbques.Text = dt.Rows[0]["security_question"].ToString();
            tbanswer.Text = dt.Rows[0]["answer"].ToString();
        }

        protected void btndelete_Command(object sender, CommandEventArgs e)
        {
            if (e.CommandName == "Delete")
            {
                DataTable dt = teach.GetRecords(Convert.ToInt64(e.CommandArgument), "", "", "", "", "", "", -99, -99, -99, "", "","","","", false, " and ");
                teach.teacher_id = Convert.ToInt64(dt.Rows[0]["teacher_id"].ToString());
                teach.teacher_name = dt.Rows[0]["teacher_name"].ToString();
                teach.date_of_birth = dt.Rows[0]["date_of_birth"].ToString();
                teach.date_of_join= dt.Rows[0]["date_of_join"].ToString();
                teach.gender = dt.Rows[0]["gender"].ToString();
                teach.photo = dt.Rows[0]["photo"].ToString();
                teach.permanent_address = dt.Rows[0]["permanent_address"].ToString();
                teach.residence_pno = Convert.ToInt64(dt.Rows[0]["residence_pno"].ToString());
                teach.mobile_pno = Convert.ToInt64(dt.Rows[0]["mobile_pno"].ToString());
                teach.user_type_id = Convert.ToInt64(dt.Rows[0]["user_type_id"].ToString());
                teach.teacher_emailid = dt.Rows[0]["teacher_emailid"].ToString();
                teach.username = dt.Rows[0]["username"].ToString();
                teach.password = dt.Rows[0]["password"].ToString();
                teach.security_question = dt.Rows[0]["security_question"].ToString();
                teach.answer = dt.Rows[0]["answer"].ToString();
                teach.isdeleted = true;
                teach.SaveRecords();
                Response.Redirect("~/admin/TeacherInfo.aspx");
            }
        }

        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {

        }

        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {

        }

        protected void btninsert_Click(object sender, EventArgs e)
        {
            teach.teacher_id = -99;
            teach.teacher_name = tbtname.Text;
            DateTime dat = new DateTime(Convert.ToInt32(ddyear.SelectedItem.ToString()), Convert.ToInt32(ddmonth.SelectedValue.ToString()), Convert.ToInt32(dddate.SelectedItem.ToString()));
            teach.date_of_birth = dat.ToString();
            DateTime dat2 = new DateTime(Convert.ToInt32(ddjyear.SelectedItem.ToString()), Convert.ToInt32(ddjmonth.SelectedValue), Convert.ToInt32(ddjdate.SelectedItem.ToString()));
            teach.date_of_join = dat2.ToString();
            teach.gender = RadioButtonList1.SelectedValue.ToString();
            if(FileUpload1.HasFile)
                FileUpload1.PostedFile.SaveAs(Server.MapPath("~/admin/teacher/"+FileUpload1.FileName));
            teach.photo=FileUpload1.FileName;
            teach.permanent_address=tbaddress.Text;
            teach.residence_pno=Convert.ToInt64(tbreno.Text);
            teach.mobile_pno=Convert.ToInt64(tbmobile.Text);
            teach.user_type_id=Convert.ToInt64(ddusertype.SelectedValue.ToString());
            teach.teacher_emailid = tbmail.Text;
            teach.username=tbusername.Text;
            teach.password=tbpassword.Text;
            teach.security_question = tbques.Text;
            teach.answer = tbanswer.Text;
            teach.isdeleted=false;
            teach.SaveRecords();
            //mail to teacher
            MailMessage MyMessage = new MailMessage();
            MailAddress SendFrom = new MailAddress("eschool123g@gmail.com");
            MyMessage.To.Add(tbmail.Text);
            MyMessage.From = SendFrom;
            MyMessage.Subject = "Your login information.";
            MyMessage.Body = tbusername.Text + "  " + tbpassword.Text;
            System.Net.NetworkCredential basicAuthenticationInfo = new System.Net.NetworkCredential("eschool123g@gmail.com", "eschool@123");

            SmtpClient emailClient1 = new SmtpClient("smtp.gmail.com");
            emailClient1.EnableSsl = true;
            emailClient1.Port = 587;
            emailClient1.Credentials = basicAuthenticationInfo;
            emailClient1.Send(MyMessage);

            Response.Redirect("~/admin/TeacherInfo.aspx");
        }

        protected void btnupdate_Click(object sender, EventArgs e)
        {
            teach.teacher_id = Convert.ToInt64(tbtid.Text);
            teach.teacher_name = tbtname.Text;
            DateTime dat = new DateTime(Convert.ToInt32(ddyear.SelectedItem.ToString()), Convert.ToInt32(ddmonth.SelectedValue.ToString()), Convert.ToInt32(dddate.SelectedItem.ToString()));
            teach.date_of_birth = dat.ToString();
            DateTime dat2 = new DateTime(Convert.ToInt32(ddjyear.SelectedItem.ToString()), Convert.ToInt32(ddjmonth.SelectedValue), Convert.ToInt32(ddjdate.SelectedItem.ToString()));
            teach.date_of_join = dat2.ToString();
            teach.gender = RadioButtonList1.SelectedValue.ToString();
            if (FileUpload1.HasFile)
            {
                FileUpload1.PostedFile.SaveAs(Server.MapPath("~/admin/teacher/" + FileUpload1.FileName));
                teach.photo = FileUpload1.FileName;
            }
            else
            teach.photo = tbphoto.Text;
            teach.permanent_address = tbaddress.Text;
            teach.residence_pno = Convert.ToInt64(tbreno.Text);
            teach.mobile_pno = Convert.ToInt64(tbmobile.Text);
            teach.user_type_id = Convert.ToInt64(ddusertype.SelectedValue.ToString());
            teach.teacher_emailid = tbmail.Text;
            teach.username = tbusername.Text;
            teach.password = tbpassword.Text;
            teach.security_question = tbques.Text;
            teach.answer = tbanswer.Text;
            teach.isdeleted = false;
            teach.SaveRecords();
            Response.Redirect("~/admin/TeacherInfo.aspx");
        }

        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView1.PageIndex = e.NewPageIndex;
            DataTable dt = teach.GetRecords(-99, "", "", "", "", "", "", -99, -99, -99, "", "", "", "", "", false, " and ");
            GridView1.DataSource = dt;
            GridView1.DataBind();
        }
    }
}
